<template>
  <div>
    <carrier :title="title">
      <div slot="top">
        <!-- 搜索框 -->
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList" @change="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="goAddPage">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <div slot="count">
        <el-table :data="goodsList" style="width: 100%" border stripe>
          <el-table-column  label="#" type="index">
          </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" >
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格 (元)" width="120px">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="120px">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="170px">
            <template v-slot="scope">
              <span>{{scope.row.add_time | dateFormat}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <template v-slot="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="editById(scope.row.goods_id)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteById(scope.row.goods_id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[6, 10, 20, 40, 60]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background>
        </el-pagination>
      </div>
    </carrier>
  </div>
</template>

<script>
  import Carrier from 'views/home/Carrier.vue'

  import { getHomeData } from 'network/home.js'

  export default {
    name: 'List',
    data() {
      return {
        title: ['商品管理', '商品列表'],
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 6
        },
        goodsList: [],
        total: 0
      }
    },
    components: {
      Carrier
    },
    created() {
      this.getGoodsList();
    },
    methods: {
      // 获取商品列表
      getGoodsList() {
        getHomeData({
          url: 'goods',
          params: this.queryInfo
        })
          .then(res => {
            // console.log(res);
            if (res.meta.status !== 200)
              return this.$message({
                type: 'error',
                message: res.meta.msg
              })
            this.goodsList = res.data.goods;
            this.total = res.data.total;
          })
      },
      handleSizeChange(new_size) {
        this.queryInfo.pagesize = new_size;
        this.getGoodsList();
      },
      handleCurrentChange(new_current) {
        this.queryInfo.pagenum = new_current;
        this.getGoodsList();
      },
      deleteById(id) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '注意', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          return getHomeData({
            url: `goods/${id}`
          }, 'delete')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          return new Promise(() => {})        
        })
        .then(res => {
          if (res.meta.status !== 200)
              return this.$message({
                type: 'error',
                message: res.meta.msg
              })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getGoodsList();
        })
      },
      goAddPage() {
        this.$router.push('/goods/add')
      },
      editById(id) {
        this.$router.push({
          path: '/goods/add',
          query: {
            id: id
          }
        })
      }
    },
  }
</script>

<style scoped>
  .el-table {
    margin-bottom: 15px;
  }
</style>